#include "AudioAction.h"
#include "audiocaptureinput.h"
#include "audioencodetask.h"

AudioAction::AudioAction(unsigned int bitrate, unsigned int samplingrate)
{
	m_oBitrate				= bitrate;
	m_oSamplingrate			= samplingrate;
	m_oAudioCaptureInput	= NULL;
	m_oAudioEncodeTask		= NULL;
}
AudioAction::~AudioAction()
{
}

bool AudioAction::OnInit(Receive_handler* handle)
{
	if (m_oAudioCaptureInput == NULL)
		m_oAudioCaptureInput = new AudioCaptureInput;

	if (m_oAudioEncodeTask == NULL)
		m_oAudioEncodeTask = new AudioEncodeTask;

	if (m_oAudioEncodeTask->init(handle, m_oSamplingrate, 2, m_oBitrate * 1000) == false)  //
		return false;

	return true;
}

bool AudioAction::OnStart()
{
	if (m_oAudioEncodeTask)
		m_oAudioEncodeTask->activate();//

	if (m_oAudioCaptureInput && m_oAudioEncodeTask)
	{
		return m_oAudioCaptureInput->start(m_oAudioEncodeTask, m_oSamplingrate);
	}
	return false;
}

bool AudioAction::OnStop()
{
	if (m_oAudioCaptureInput)
		m_oAudioCaptureInput->stop();

	if (m_oAudioEncodeTask)
		m_oAudioEncodeTask->close();

	//------------- delete --------------
	if (m_oAudioCaptureInput)
		delete m_oAudioCaptureInput;
	m_oAudioCaptureInput = NULL;

	if (m_oAudioEncodeTask)
		delete m_oAudioEncodeTask;
	m_oAudioEncodeTask = NULL;

	return true;
}

std::string AudioAction::getAudioHeader()
{
	if (m_oAudioEncodeTask)
		return m_oAudioEncodeTask->getAudioHeader();
	return "";
}

float AudioAction::getFrameDuration()
{
	if (m_oAudioEncodeTask)
		return m_oAudioEncodeTask->getFrameDuration();
	return 0.0;
}
